package util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import android.util.Log;

public class DateUtil {
	private long year;
	private long month;
	private long day;
	private long hour;
	private long minute;
	private long second;
	public DateUtil(){
		year = 0;
		month = 0;
		day = 0;
		hour = 0;
		minute = 0;
		second = 0;
		
	}
	public void getDiffDate(String sDate){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar server=new GregorianCalendar(TimeZone.getTimeZone("GMT+7:00"));
		server.setTimeInMillis(new Date().getTime());
		Calendar client=Calendar.getInstance();
		client.setTimeInMillis(new Date().getTime());
		try {
			Date d1 = dateFormat.parse(sDate);
			//int dayDiff1=client.get(Calendar.DAY_OF_MONTH) - server.get(Calendar.DAY_OF_MONTH);
			int hourDiff1=client.get(Calendar.HOUR_OF_DAY) - server.get(Calendar.HOUR_OF_DAY);
//			if(dayDiff1 > 0)
//				hourDiff1 += 24;
			long diff1 = hourDiff1 * 60 * 60 * 1000;
			long diff = System.currentTimeMillis() - d1.getTime() + diff1;
		
//			Log.d("tag","diff1:"+diff1);
			second = diff / 1000;
			if(second >= 60)
				minute = second / 60;
			if(minute >= 60)
				hour = minute / 60;
			if(hour >= 24)
				day = hour / 24;
			if(day >= 30)
				month = day / 30;
			if(month >= 12)
				year = month / 12;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String fomatDate(String time){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:");
		DateFormat outputFormat = new SimpleDateFormat("hh:mm a . dd MMM y");
		Calendar server=new GregorianCalendar(TimeZone.getTimeZone("GMT+7:00"));
		server.setTimeInMillis(new Date().getTime());
		Calendar client=Calendar.getInstance();
		client.setTimeInMillis(new Date().getTime());
		try {
			Date d1 = dateFormat.parse(time);
			int hourDiff1 = client.get(Calendar.HOUR_OF_DAY) - server.get(Calendar.HOUR_OF_DAY);
			long diff1 = hourDiff1 * 60 * 60 * 1000;
			long localMiliseconds = d1.getTime() + diff1;
		
			Date outputDate = new Date(localMiliseconds);
			return outputFormat.format(outputDate);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "error";
	}
	public void resetTime(){
		year = 0;
		month = 0;
		day = 0;
		hour = 0;
		minute = 0;
		second = 0;
	}
	public long getYear() {
		return year;
	}
	public void setYear(long year) {
		this.year = year;
	}
	public long getMonth() {
		return month;
	}
	public void setMonth(long month) {
		this.month = month;
	}
	public long getDay() {
		return day;
	}
	public void setDay(long day) {
		this.day = day;
	}
	public long getHour() {
		return hour;
	}
	public void setHour(long hour) {
		this.hour = hour;
	}
	public long getMinute() {
		return minute;
	}
	public void setMinute(long minute) {
		this.minute = minute;
	}
	public long getSecond() {
		return second;
	}
	public void setSecond(long second) {
		this.second = second;
	}
}
